package com.raysdata.smartcity.dao.sys;

import com.raysdata.smartcity.dao.RzBaseDao;
import com.raysdata.smartcity.dto.Attachment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by bh on 2016/11/14.
 */
@Component
public class AttachDao{
    @Autowired
    private RzBaseDao rzBaseDao;

    public long insert(Attachment attachment){

        StringBuilder sql = new StringBuilder();
        sql.append("insert into attachment  ( attachment_id,name,code,path,f_type,status ) ");
        sql.append("values ( :attachment_id,");
        sql.append(":name,:code,:path,:f_type,:status)");
        return rzBaseDao.insert(sql.toString(), attachment);
    }

    public int update(Attachment attachment){
        String sql="update attachment set fid=:fid " +
                "where attachment_id=:attachment_id ";
        return rzBaseDao.update(sql,attachment);
    }

    public Attachment getAttachmentByid(String id){
        String sql="select * from attachment " +
                "where attachment_id=?";
        return rzBaseDao.queryById(sql,Attachment.class,id);
    }

    public List<Attachment> getAllByFid(String fid){
        String sql="select * from attachment " +
                "where status='0' and fid=?";
        return rzBaseDao.query(sql,Attachment.class,new Object[]{fid});
    }

    public int delete(String id){
        String sql="DELETE FROM attachment where attachment_id='"+id+"'";
        return rzBaseDao.delete(sql);
    }

    public int deleteByFid(String id){
        String sql="DELETE FROM attachment where fid='"+id+"'";
        return rzBaseDao.delete(sql);
    }
}
